package cn.mickey.exchange.api;


import cn.mickey.exchange.annotation.LoginUser;
import cn.mickey.exchange.annotation.RedisCache;
import cn.mickey.exchange.domain.JsonResponse;
import cn.mickey.exchange.domain.model.MessageModel;
import cn.mickey.exchange.domain.model.UserModel;
import cn.mickey.exchange.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RedisCache
@RestController
@RequestMapping("/message")
public class MessageApi {

    @Resource
    private MessageService messageService;

    @PostMapping("/send")
    public JsonResponse sendMessage(@LoginUser UserModel user,
                                @RequestBody MessageModel messageModel){
        messageModel.setUserId(user.getUserId());
        if(messageService.addMessage(messageModel)){
            return JsonResponse.success(messageModel);
        }
        return JsonResponse.fail("系统错误!");
    }

    @GetMapping("/info")
    public JsonResponse getMessage(@RequestParam Long messageId){
        return JsonResponse.success(messageService.getMessageById(messageId));
    }

    @GetMapping("/idle")
    public JsonResponse getAllIdleMessage(@RequestParam Long idleId){
        return JsonResponse.success(messageService.getAllIdleMessage(idleId));
    }

    @GetMapping("/my")
    public JsonResponse getAllyMessage(@LoginUser UserModel user){
        return JsonResponse.success(messageService.getAllMyMessage(user.getUserId()));
    }

    @GetMapping("/delete")
    public JsonResponse deleteMessage(@LoginUser UserModel user,
                                  @RequestParam Long id){
        if(messageService.deleteMessage(id)){
            return JsonResponse.success();
        }
        return JsonResponse.fail("系统错误!");
    }
}
